<?php
namespace App\Repositories;

use App\Note;
use App\Record;
use App\Repositories\BaseRepository;

class NoteRepository {
	use BaseRepository;

	protected $model, $record, $table_name;

	public function __construct(Note $note, Record $record) {
		$this->model = $note;
		$this->record = $record;
		$this->table_name = 'notes';
	}

	protected function searchColumn($query, $where) {
		return $query->when(array_key_exists('title', $where), function ($query) use ($where) {
			return $query->where('title', 'like', "%{$where['title']}%");
		});
	}

	public function getNotes() {
		$data = $this->model->selectRaw('id, (percent / num) as scale, title, image, briefly, percent, num, type')->orderBy('scale', 'desc')->get();
		$end_list = [
			'finish' => [],
			'reading' => [],
			'want' => [],
			'record' => [],
		];
		foreach ($data as $key => $value) {
			if ($value['type'] == 0) {
				array_push($end_list['want'], $value);
				continue;
			}
			if ($value['type'] == 2) {
				array_push($end_list['record'], $value);
				continue;
			}
			if ($value['scale'] >= 1) {
				array_push($end_list['finish'], $value);
				continue;
			}
			array_push($end_list['reading'], $value);
		}
		unset($data);
		return $end_list;
	}
}